package com.oocl.servlet;

import static com.oocl.util.IPPropertiesUtil.getProperty;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.pojo.Constants;
import com.oocl.pojo.Dish;
import com.oocl.pojo.DishType;
import com.oocl.pojo.Merchant;
import com.oocl.pojo.Protocol;
import com.oocl.service.DishManager;
import com.oocl.service.DishTypeManager;
import com.oocl.service.MerchantManager;
import com.oocl.service.factory.DishManagerFactory;
import com.oocl.service.factory.DishTypeManagerFactory;
import com.oocl.service.factory.MerchantManagerFactory;
import com.oocl.util.DESUtil;
import com.oocl.util.HTTPConnUtil;
import com.oocl.util.ValidateUtil;

@WebServlet("/sec/dishes")
public class DishesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private final String GET_MERCHANTS_URL = String.format("http://%s:%s/O2OAdministrator/getMerchant", getProperty("admin.ipaddr"), getProperty("admin.port"));
//	private final String GET_MERCHANTS_URL = "http://10.222.29.201:8080/O2OAdministrator/getMerchant";
//	private final String GET_MERCHANTS_URL = "http://10.222.29.178:9090/O2OAdministrator/getMerchant";
	
	private DishManager dishManager = DishManagerFactory.getInstance();
	private DishTypeManager dishTypeManager = DishTypeManagerFactory.getInstance();
	private MerchantManager merchantManager = MerchantManagerFactory.getInstance();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// fetch list<mid> by sending http request to A-server
		// mock a id list here
//		List<String> allAllowedMid = new ArrayList<>();
//		allAllowedMid.add("de4de322-11a8-440c-ae52-be8e191f5ee7");
		
		String merchantsJson = HTTPConnUtil.sendGet(GET_MERCHANTS_URL);
		
		if (ValidateUtil.isNullOrEmpty(merchantsJson)) {
			request.setAttribute("backHref", "home");
			request.setAttribute("errorMsg", "获取商家列表失败!");
			request.getRequestDispatcher("/WEB-INF/views/error.jsp").forward(request, response);
			return;
		}
		merchantsJson = merchantsJson.replaceAll(" ", "+");
		merchantsJson = new DESUtil().decrypt(merchantsJson,"UTF-8");
		Protocol p = new ObjectMapper().readValue(merchantsJson, Protocol.class);
		
		@SuppressWarnings("unchecked")
		List<String> listObj = (List<String>) p.getMsgObj();
		List<String> allAllowedMid = merchantManager.MerchantsIdFilterByStatus(listObj);
		
		String mId = request.getParameter("mid");
		if (ValidateUtil.isNullOrEmpty(mId)) {
			response.sendRedirect("home");
			return;
		}
		
		Merchant m = merchantManager.findOneMerchantById(mId);
		
		if (m == null || !m.getmStatus().equals(Constants.APPLY_PASSED) || !allAllowedMid.contains(m.getmId())) {
			request.setAttribute("backHref", "home");
			request.setAttribute("errorMsg", "你所请求的商家不存在/未过审/或已被拉黑!");
			request.getRequestDispatcher("/WEB-INF/views/error.jsp").forward(request, response);
			return;
		}
		
		request.setAttribute("merchant", m);
		
		List<DishType> dishTypes = dishTypeManager.getAllDishTypes();
		request.setAttribute("dishTypes", dishTypes);
		
		List<Dish> dishes = dishManager.findDishes(0, null, mId);
		request.setAttribute("dishes", dishes);
		
		request.getRequestDispatcher("/WEB-INF/views/dishes.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
